package cn.wolfcode.wolf2w.service.impl;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.query.TravelCondition;
import cn.wolfcode.wolf2w.query.TravelQueryObject;
import cn.wolfcode.wolf2w.repository.TravelRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelRepository repsitory;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IDestinationService destinationService;

    @Override
    public void save(Travel travel) {
        travel.setId(null);
        //根据主题id查询主题对象

        repsitory.save(travel);
    }

    @Override
    public void update(Travel travel) {
        repsitory.save(travel);
    }

    @Override
    public void delete(String id) {
        repsitory.deleteById(id);
    }

    @Override
    public Page<Travel> query(TravelQueryObject qo) {
        //分页查询
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1,qo.getPageSize(), Sort.Direction.DESC,"releaseTime");
        if (StringUtils.hasLength(qo.getDestId())) {
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        //花费金额
        TravelCondition per = TravelCondition.PEREXPENDMAP.get(qo.getPerExpendType());
        if (per != null) {
            query.addCriteria(Criteria.where("perExpend").gte(per.getMin()).lte(per.getMax()));
        }
        //旅游天数
        TravelCondition day = TravelCondition.DAYMAP.get(qo.getDayType());
        if (day != null) {
            query.addCriteria(Criteria.where("day").gte(day.getMin()).lte(day.getMax()));
        }

        if (qo.getOrderType() == QueryObject.ORDER_HOT) {
             pageable = PageRequest.of(qo.getCurrentPage() - 1,qo.getPageSize(), Sort.Direction.DESC,"viewnum");
        } else if (qo.getOrderType() == QueryObject.ORDER_NEW) {
             pageable = PageRequest.of(qo.getCurrentPage() - 1,qo.getPageSize(), Sort.Direction.DESC,"releaseTime");
        }
        Page<Travel> page = DBHelper.query(mongoTemplate, pageable, query, Travel.class, qo);
        for (Travel travel : page) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
        }
        return page;
    }

    @Override
    public void audit(String id, int state) {
        Travel travel = this.get(id);
        if (travel == null || travel.getState() != Travel.STATE_WAITING) {
            throw new LogicException("参数异常");
        }
        if (state == Travel.STATE_RELEASE) {
            //修改游记状态为审核通过
            travel.setState(Travel.STATE_RELEASE);
            //设置发布时间
            travel.setReleaseTime(new Date());
            //设置最后更新时间
            travel.setLastUpdateTime(new Date());
        } else {
            //修改游记状态为拒绝通过
            travel.setState(Travel.STATE_REJECT);
            //设置游记发布时间为null
            travel.setReleaseTime(null);
            //设置最后更新时间
            travel.setLastUpdateTime(new Date());
        }
        //保存修改之后的数据
        this.update(travel);
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        //目的地的名称
        Destination dest = destinationService.get(travel.getDestId());
        travel.setDestName(dest.getName());

        travel.setLastUpdateTime(new Date());

       //用户id


        if (StringUtils.hasLength(travel.getId())) {
            this.update(travel);
        } else {
            travel.setCreateTime(new Date());
            this.save(travel);
        }
    }

    @Override
    public List<Travel> queryViewNum(String destId) {
        Pageable pageable = PageRequest.of(0,3,Sort.Direction.DESC,"viewnum");
        return repsitory.findByDestId(destId, pageable);
    }

    @Override
    public Travel get(String id) {
        Optional<Travel> op = repsitory.findById(id);
        Travel travel = null;
        if (op.isPresent()) {
            travel = op.get();
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            travel.setDest(destinationService.get(travel.getDestId()));
        }
        return travel;
    }

    @Override
    public List<Travel> list() {
        return repsitory.findAll();

    }
}
